package cassys.domain.assessment;

import cassys.domain.classManagement.Student;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * @author User
 */
public class AssessmentGroup implements Serializable {

    private long assessmentGroupId;
    private String title;
    private double markObtained;
    private Assessment assessment;
    private Set<Student> members;
    private Collection<EvaluationItem> evaluationItems;

    public AssessmentGroup() {
        this.members = new HashSet<Student>();
        this.evaluationItems = new ArrayList<EvaluationItem>();
    }

    public AssessmentGroup(Assessment assessment, String title) {
        this();
        this.title = title;
        this.assessment = assessment;
        populateEvaluationItems();
    }

    public void addMember(Student student) {
        this.getMembers().add(student);
        student.signUpAssessmentGroup(this);
    }

    int getGroupSize() {
        return getMembers().size();
    }

    void mark(AssessmentItem markItem, int mark) throws IllegalMarkException {
        if (this.assessment.validateMarkRange(mark)) {


            for (EvaluationItem evaluationItem : evaluationItems) {
                if (evaluationItem.getAssessmentItem().getName().equals(markItem.getName())) {
                    evaluationItem.setMarkObtained(mark);
                }
            }
        }
    }

    private void populateEvaluationItems() {
        for (AssessmentItem item : this.assessment.getAccessmentItems()) {
            this.evaluationItems.add(new EvaluationItem(item));
        }
    }

    public double evaluate() {
        return this.assessment.evaluate(this.evaluationItems);
    }

    /**
     * @return the title
     */
    public String getTitle() {
        return title;
    }

    /**
     * @param title the title to set
     */
    public void setTitle(String title) {
        this.title = title;
    }

    /**
     * @return the evaluationItems
     */
    public Collection<EvaluationItem> getEvaluationItems() {
        return evaluationItems;
    }

    /**
     * @param evaluationItems the evaluationItems to set
     */
    public void setEvaluationItems(Collection<EvaluationItem> evaluationItems) {
        this.evaluationItems = evaluationItems;
    }

    public Set<Student> getMembers() {
        return members;
    }

    public void setMembers(Set<Student> members) {
        this.setMembers(members);
    }
    
    public void addStudent(Student student) {
        if (!getMembers().contains(student)) {
            getMembers().add(student);
        }
        if (!student.isMemberOf(this)) {
            student.signUpAssessmentGroup(this);
        }
    }
	
    public void removeStudent(Student student){
	if(getMembers().contains(student)){
            getMembers().remove(student);
	}
    }


  public long getAssessmentGroupId() {
        return assessmentGroupId;
    }

    public void setAssessmentGroupId(long assessmentGroupId) {
        this.assessmentGroupId = assessmentGroupId;
    }

    /**
     * @return the markObtained
     */
    public double getMarkObtained() {
        return markObtained;
    }

    /**
     * @param markObtained the markObtained to set
     */
    public void setMarkObtained(double markObtained) {
        this.markObtained = markObtained;
    }

   
    
}
